using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GameMap;
using System.Xml.Serialization;
using System.IO;

namespace MapEditor
{
    public partial class MainForm : Form
    {
        StationsNetwork gameMap = new StationsNetwork();

        public MainForm()
        {
            InitializeComponent();

            treeView.Nodes.Add(new TreeNode("Stations Network"));
        }

        private void LoadMapButton_Click(object sender, EventArgs e)
        {
            if (LoadMapDialog.ShowDialog() == DialogResult.OK)
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(StationsNetwork));
                // To read from a file we create a StreamReader object.
                StreamReader myReader = new StreamReader(LoadMapDialog.FileName);
                gameMap = (StationsNetwork)mySerializer.Deserialize(myReader);
                myReader.Close();

                RefreshTreeView(gameMap);
            }
        }

        private void SaveMapAsButton_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == SaveMapDialog.ShowDialog())
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(StationsNetwork));
                // To write to a file, create a StreamWriter object.
                StreamWriter myWriter = new StreamWriter(SaveMapDialog.FileName);
                mySerializer.Serialize(myWriter, gameMap);
                myWriter.Close();
            }
        }

        private void AddStationButton_Click(object sender, EventArgs e)
        {
            int stationID;
            Station newStation = new Station();

            if (int.TryParse(StationIDTextBox.Text, out stationID))
            {
                newStation.StationID = stationID;
            }
            else
            {
                MessageBox.Show("Station ID needs to be a number");
                return;
            }
            
            if(!string.IsNullOrEmpty(TaxiConnectionsTextBox.Text))
            {
                AddConnections(newStation.TaxiConnections, TaxiConnectionsTextBox.Text);
            }

            if(!string.IsNullOrEmpty(BusConnectionsTextBox.Text))
            {
                AddConnections(newStation.BusConnections, BusConnectionsTextBox.Text);
            }
            if(!string.IsNullOrEmpty(TubeConnectionsTextBox.Text))
            {
                AddConnections(newStation.UndergroundConnections, TubeConnectionsTextBox.Text);
            }
            if(!string.IsNullOrEmpty(BoatConnectionsTextBox.Text))
            {
                AddConnections(newStation.BoatConnections, BoatConnectionsTextBox.Text);
            }

            gameMap.AddStation(newStation);
            AddToTreeView(newStation);
        }

        private void AddToTreeView(Station newStation)
        {
            TreeNode root = treeView.Nodes[0];
            TreeNode newStationNode = new TreeNode("Station " + newStation.StationID);


            AddConnectionsSubNode(newStationNode, newStation.TaxiConnections, "Taxi Connections");
            AddConnectionsSubNode(newStationNode, newStation.BusConnections, "Bus Connections");
            AddConnectionsSubNode(newStationNode, newStation.UndergroundConnections, "Tube Connections");
            AddConnectionsSubNode(newStationNode, newStation.BoatConnections, "Boat Connections");

            root.Nodes.Add(newStationNode);
        }

        private static void AddConnectionsSubNode(TreeNode newNode, List<int> connectionsList, string label)
        {
            if (connectionsList.Count > 0)
            {
                string connectionsNodeText = "";
                connectionsNodeText += label + ": ";
                for (int i = 0; i < connectionsList.Count - 1; i++)
                {
                    connectionsNodeText += connectionsList[i].ToString() + ", ";
                }
                connectionsNodeText += connectionsList[connectionsList.Count - 1].ToString();

                newNode.Nodes.Add(new TreeNode(connectionsNodeText)); 
            }
        }

        private void RefreshTreeView(StationsNetwork gameMap)
        {
            foreach (Station station in gameMap.Stations)
            {
                AddToTreeView(station);
            }
        }

        private void AddConnections(List<int> connectionsList, string text)
        {
            char[] seperators = { ',', ' ' };

            foreach (string s in text.Split(seperators, StringSplitOptions.RemoveEmptyEntries))
            {
                int i;
                if (int.TryParse(s, out i))
                {
                    connectionsList.Add(i);
                }
            }
        }

        private void connectionsTextBox_TextChanged(object sender, EventArgs e)
        {
            string stringToValidate = (sender as TextBox).Text;

            char[] seperators = {',',' '};

            foreach (string s in stringToValidate.Split(seperators,StringSplitOptions.RemoveEmptyEntries))
            {
                int i;
                if (int.TryParse(s, out i))
                {
                    continue;
                }
                else
                {
                    if(!string.IsNullOrEmpty(s))
                    {
                        MessageBox.Show("Numbers only fool!");
                        string textboxCurrentText = (sender as TextBox).Text;
                        string prev = textboxCurrentText.Remove(textboxCurrentText.Length - 1);
                        (sender as TextBox).Text = prev;
                    }
                }
            }
        }
    }
}